package tema.pdm;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Descriere extends Activity implements ThreadDone{
	
	TextView data, conditions, gradeC,
			Humidity, wind_condition,
			day1, gradeC1, Condition1,
			day2, gradeC2, Condition2, 
			day3, gradeC3, Condition3,
			day4, gradeC4, Condition4,
			orasView;
	ImageView Icon0,Icon1, Icon2, Icon3, Icon4;
	String oras;
	Button update;
	Context context;
	int contor;
	ProgressDialog progres;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.description);
        
        Intent intent = this.getIntent();
        oras = intent.getStringExtra("nume");
        orasView = (TextView)findViewById(R.id.oras);
        orasView.setText(oras);
        
        context = this;
        progres = new ProgressDialog(context);
        progres.setMessage("Updating. Please wait...");
        
        data = (TextView)findViewById(R.id.data); 
        conditions = (TextView)findViewById(R.id.conditions); 
        gradeC = (TextView)findViewById(R.id.gradeC); 
        Humidity = (TextView)findViewById(R.id.Humidity); 
        wind_condition = (TextView)findViewById(R.id.wind_condition); 
        day1 = (TextView)findViewById(R.id.day1); 
        gradeC1 = (TextView)findViewById(R.id.gradeC1); 
        Condition1 = (TextView)findViewById(R.id.Condition1); 
        day2 = (TextView)findViewById(R.id.day2); 
        gradeC2 = (TextView)findViewById(R.id.gradeC2); 
        Condition2 = (TextView)findViewById(R.id.Condition2); 
        day3 = (TextView)findViewById(R.id.day3); 
        gradeC3 = (TextView)findViewById(R.id.gradeC3); 
        Condition3 = (TextView)findViewById(R.id.Condition3); 
        day4 = (TextView)findViewById(R.id.day4); 
        gradeC4 = (TextView)findViewById(R.id.gradeC4); 
        Condition4 = (TextView)findViewById(R.id.Condition4); 
        
        Icon0 = (ImageView)findViewById(R.id.Icon0);
        Icon1 = (ImageView)findViewById(R.id.Icon1);
        Icon2 = (ImageView)findViewById(R.id.Icon2);
        Icon3 = (ImageView)findViewById(R.id.Icon3);
        Icon4 = (ImageView)findViewById(R.id.Icon4);
        
        update = (Button)findViewById(R.id.UpdateazaDescrierea);
        update.setOnClickListener(new OnClickListener(){
    		@Override
    		public void onClick(View v) {
    			progres.show();
    			Update t = new Update(0,oras,context);
    			t.start();
    			afiseaza();
    		}
        });
        
        afiseaza();
        
	}

public void threadDone()
{
	afiseaza();
	progres.dismiss();
}
	
	void afiseaza()
	{
		try{
	        File file =new File( Environment.getExternalStorageDirectory() + "/Tema1PDM/" + oras + ".txt");
	        
	        // parsare
	        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();

			//forecast_information
			
			NodeList nodeList = doc.getElementsByTagName("forecast_information");
			System.out.println("cica totul e bine.."+ nodeList.getLength());
			NodeList nodsList= nodeList.item(0).getChildNodes();
			Node  nod;
			int i;
			for(i =0; i<nodsList.getLength();i++)
			{
				nod = nodsList.item(i);
				String date =nod.getNodeName();
				if(date.equals("forecast_date"))
					data.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				date = date + "||"+ nod.getAttributes().getNamedItem("data").getNodeValue();
				System.out.println("data:"+date);
			}
			
			// current_conditions
			
			nodeList = doc.getElementsByTagName("current_conditions");
			System.out.println("cica totul e bine.."+ nodeList.getLength());
			nodsList= nodeList.item(0).getChildNodes();
			
			for(i =0; i<nodsList.getLength();i++)
			{
				nod = nodsList.item(i);
				String date =nod.getNodeName();
				
				if(date.equals("condition"))
					conditions.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date.equals("temp_c"))
					gradeC.setText(nod.getAttributes().getNamedItem("data").getNodeValue()+" grade Celsius");
				
				if(date.equals("humidity"))
					Humidity.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date.equals("wind_condition"))
					wind_condition.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date.equals("icon"))
				{
					String iconita = nod.getAttributes().getNamedItem("data").getNodeValue();
					iconita = iconita.substring(19);
					iconita = iconita.substring(0, iconita.length() - 4);
					System.out.println("iconita:"+iconita);
					int resID = getResources().getIdentifier(iconita, "drawable", getPackageName());
					Icon0.setImageResource(resID);
				}
			}
			
			// forecast_conditions
			
			nodeList = doc.getElementsByTagName("forecast_conditions");
			System.out.println("cica totul e bine.."+ nodeList.getLength());
			nodsList= nodeList.item(0).getChildNodes();
			
			for(i =0; i<nodsList.getLength();i++)
			{
				nod = nodsList.item(i);
				String date1 =nod.getNodeName();
				
				if(date1.equals("condition"))
					Condition1.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date1.equals("low"))
				{
					String grad = nod.getAttributes().getNamedItem("data").getNodeValue();
					int c = ( Integer.parseInt(grad) - 32)*5/9;
					gradeC1.setText(c+ " grade Celsius");
				}
					
				if(date1.equals("day_of_week"))
					day1.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date1.equals("icon"))
				{
					String iconita = nod.getAttributes().getNamedItem("data").getNodeValue();
					iconita = iconita.substring(19);
					iconita = iconita.substring(0, iconita.length() - 4);
					System.out.println("iconita:"+iconita);
					int resID = getResources().getIdentifier(iconita, "drawable", getPackageName());
					Icon1.setImageResource(resID);
				}

			
			date1 = date1 + "||"+ nod.getAttributes().getNamedItem("data").getNodeValue();
			System.out.println("data:"+date1);
			}

			
			nodsList= nodeList.item(1).getChildNodes();
			
			for(i =0; i<nodsList.getLength();i++)
			{
				nod = nodsList.item(i);
				String date1 =nod.getNodeName();
				
				if(date1.equals("condition"))
					Condition2.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date1.equals("low"))
				{
					String grad = nod.getAttributes().getNamedItem("data").getNodeValue();
					int c = ( Integer.parseInt(grad) - 32)*5/9;
					gradeC2.setText(c+ " grade Celsius");
				}
					
				if(date1.equals("day_of_week"))
					day2.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date1.equals("icon"))
				{
					String iconita = nod.getAttributes().getNamedItem("data").getNodeValue();
					iconita = iconita.substring(19);
					iconita = iconita.substring(0, iconita.length() - 4);
					System.out.println("iconita:"+iconita);
					int resID = getResources().getIdentifier(iconita, "drawable", getPackageName());
					Icon2.setImageResource(resID);
				}

			
			date1 = date1 + "||"+ nod.getAttributes().getNamedItem("data").getNodeValue();
			System.out.println("data:"+date1);
			}
			
			
			nodsList= nodeList.item(2).getChildNodes();
			
			for(i =0; i<nodsList.getLength();i++)
			{
				nod = nodsList.item(i);
				String date1 =nod.getNodeName();
				
				if(date1.equals("condition"))
					Condition3.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date1.equals("low"))
				{
					String grad = nod.getAttributes().getNamedItem("data").getNodeValue();
					int c = ( Integer.parseInt(grad) - 32)*5/9;
					gradeC3.setText(c+ " grade Celsius");
				}
					
				if(date1.equals("day_of_week"))
					day3.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date1.equals("icon"))
				{
					String iconita = nod.getAttributes().getNamedItem("data").getNodeValue();
					iconita = iconita.substring(19);
					iconita = iconita.substring(0, iconita.length() - 4);
					System.out.println("iconita:"+iconita);
					int resID = getResources().getIdentifier(iconita, "drawable", getPackageName());
					Icon3.setImageResource(resID);
				}

			
			date1 = date1 + "||"+ nod.getAttributes().getNamedItem("data").getNodeValue();
			System.out.println("data:"+date1);
			}
			
			
			nodsList= nodeList.item(3).getChildNodes();
			
			for(i =0; i<nodsList.getLength();i++)
			{
				nod = nodsList.item(i);
				String date1 =nod.getNodeName();
				
				if(date1.equals("condition"))
					Condition4.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date1.equals("low"))
				{
					String grad = nod.getAttributes().getNamedItem("data").getNodeValue();
					int c = ( Integer.parseInt(grad) - 32)*5/9;
					gradeC4.setText(c+ " grade Celsius");
				}
					
				if(date1.equals("day_of_week"))
					day4.setText(nod.getAttributes().getNamedItem("data").getNodeValue());
				
				if(date1.equals("icon"))
				{
					String iconita = nod.getAttributes().getNamedItem("data").getNodeValue();
					iconita = iconita.substring(19);
					iconita = iconita.substring(0, iconita.length() - 4);
					System.out.println("iconita:"+iconita);
					int resID = getResources().getIdentifier(iconita, "drawable", getPackageName());
					Icon4.setImageResource(resID);
				}

			
			date1 = date1 + "||"+ nod.getAttributes().getNamedItem("data").getNodeValue();
			System.out.println("data:"+date1);
			}
        }catch(Exception e){e.printStackTrace();}
	}
}
